package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbListKlarifikasiBean;
import id.net.iconpln.bb.client.model.Stock;
import id.net.iconpln.bb.client.model.Waktu;
import id.net.iconpln.bb.client.service.ApprovalService;
import id.net.iconpln.bb.client.service.ApprovalServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageRekapBbBACetak extends LayoutContainer {
	
	private final ApprovalServiceAsync service = GWT.create(ApprovalService.class);
	private FormBinding formBindings;  
	private FormData formData;
	private FormPanel panel;
	ListStore store;
	private Grid grid;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
	    store = new ListStore();
	    formData = new FormData("-20"); 
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeaderVisible(false);  
	    cp.setFrame(true);  
	    cp.setSize(900, 400);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGridPltu();  
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	        new Listener<SelectionChangedEvent<Stock>>() {  
	          public void handleEvent(SelectionChangedEvent<Stock> be) {  
	            if (be.getSelection().size() > 0) {  
	              formBindings.bind((ModelData) be.getSelection().get(0));  
	            } else {  
	              formBindings.unbind();  
	            }  
	          }  
	        });  	     
	  
	    FormPanel panel = createForm();  
	    formBindings = new FormBinding(panel, true);  
	    formBindings.setStore((Store) grid.getStore());  
	  
	    cp.add(panel, new RowData(.4, 1));
	    cp.add(grid, new RowData(.6, 1));
	    
	    
//		setScrollMode(Scroll.AUTO);		
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    panel = new FormPanel();	    
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("BA Pemotongan Pembayaran Jasa Dermaga");  
	    fieldSet.setCollapsible(false);
	    
	  
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(120);  
	    fieldSet.setLayout(layout);  

	    final TextField<String> date = new TextField<String>();  
	    date.setName("");  
	    date.setFieldLabel("Hari, Tanggal");    
	    fieldSet.add(date, formData);
	    
	    TextField<String> noBA = new TextField<String>();  
	    noBA.setName("");  
	    noBA.setFieldLabel("No Berita Acara");    
	    fieldSet.add(noBA, formData);	
	    
	    final TextField<String> sla = new TextField<String>();  
	    sla.setName("");  
	    sla.setFieldLabel("Pihak SLA");    
	    fieldSet.add(sla, formData);

	    TextField<String> pemasok = new TextField<String>();	    
	    pemasok.setName("");  
	    pemasok.setFieldLabel("Pihak Pemasok");    
	    fieldSet.add(pemasok, formData);	    	    

	    TextField<String> jabpemasok = new TextField<String>();
	    jabpemasok.setName("");  
	    jabpemasok.setFieldLabel("Jabatan Pemasok");	    
	    fieldSet.add(jabpemasok, formData);

	    TextField<String> alamatpemasok = new TextField<String>();
	    alamatpemasok.setName("");  
	    alamatpemasok.setFieldLabel("Alamat Pemasok");	    
	    fieldSet.add(alamatpemasok, formData);
	    
	    TextField<String> kontrak = new TextField<String>();
	    kontrak.setValue("");  
	    kontrak.setFieldLabel("Kontrak");
	    fieldSet.add(kontrak, formData);
	    
	    TextField<String> jumlah = new TextField<String>();
	    jumlah.setValue("");  
	    jumlah.setFieldLabel("Jumlah Pengiriman");
	    fieldSet.add(jumlah, formData);
	    
	    TextField<String> kapal = new TextField<String>();
	    kapal.setValue("");  
	    kapal.setFieldLabel("Kapal");
	    fieldSet.add(kapal, formData);
	    
	    TextField<String> harga = new TextField<String>();
	    harga.setValue("");  
	    harga.setFieldLabel("Harga Jasa Dermaga");
	    fieldSet.add(harga, formData);
	    	

	    panel.add(fieldSet);  	     

	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {  
		    	    
		      }  
		    }));  

	    panel.add(p);
	    
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        panel.clear();
	      }  
	    }));  
	    
	    panel.setScrollMode(Scroll.AUTO);
	    
	    return panel;  
	  }  
	  
	  
	  
	  
	  private Grid createGridPltu(){
		    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

		    ColumnConfig column = new ColumnConfig(); 
		    
		    column = new ColumnConfig("", "Hari, Tanggal", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "No Pengajuan", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Pihak SLA", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Pihak Pemasok", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Jabatan Pihak Pemasok", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Alamat Pemasok", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Kontrak", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Jumlah Pengiriman", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Kapal", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("", "Harga Jasa Dermaga", 150);  
		    configs.add(column); 
		    
		    store.setMonitorChanges(true); 
		    ColumnModel cm = new ColumnModel(configs);  
		  
		    grid = new Grid(store, cm);  
		    grid.getView().setEmptyText("Data Empty");  
		    grid.setBorders(true);  
		  
		    return grid;  
	  }   
	  
	 
}
